<template>
  <div ref="chart" :style="{ width: '100%', height: '500px' }"></div>
</template>


<script lang="ts" setup>
import * as echarts from 'echarts'
import {  ref,  onMounted, onBeforeUnmount, nextTick} from "vue";
const chart = ref(null)

const option = ref({
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      data: [120, 200, 150, 80, 70, 110, 130],
      type: 'bar',
      showBackground: true,
      backgroundStyle: {
        color: 'rgba(180, 180, 180, 0.2)'
      }
    }
  ]
})

let myChart:any 

const initChart = () => {
  myChart.resize()
  console.log('检测resize事件是否销毁')
}

onMounted(() => {
  // mounted中初始化charts
  myChart = echarts.init(chart.value as unknown as HTMLElement)
  myChart.setOption(option.value)
  // resize事件
  window.addEventListener("resize", initChart)
})


onBeforeUnmount(() => {
  // 销毁resize事件
  window.removeEventListener('resize', initChart)
})


</script>